package visao;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;

import modelo.Mundo;

/**
 * O frame do aplicativo
 * 
 * @author jean
 * @author diego
 */
public class Tela extends JFrame {
    
    private static final long serialVersionUID = -7558568258989758597L;
    
    private Mundo renderer;
    
    /**
     * Cria a tela
     */
    public Tela() {
	super("Trabalho N3");
	setMenuBar(getMenu());
	Dimension dimensao = new Dimension(400, 400);
	renderer = new Mundo(dimensao);
	// setBounds(10, 10, 400, 400);
	
	setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	getContentPane().setLayout(new BorderLayout());
	
	/*
	 * Cria um objeto GLCapabilities para especificar o numero de bits por
	 * pixel para RGBA
	 */
	GLCapabilities glCaps = new GLCapabilities();
	glCaps.setRedBits(8);
	glCaps.setBlueBits(8);
	glCaps.setGreenBits(8);
	glCaps.setAlphaBits(8);
	
	/*
	 * Cria um canvas, adiciona ao frame e objeto "ouvinte" para os eventos
	 * Gl, de mouse e teclado
	 */
	GLCanvas canvas = new GLCanvas(glCaps);
	add(canvas, BorderLayout.CENTER);
	canvas.addGLEventListener(renderer);
	canvas.addKeyListener(renderer);
	canvas.addMouseListener(renderer);
	canvas.addMouseMotionListener(renderer);
	canvas.requestFocus();
	setSize((int) dimensao.getWidth(), (int) dimensao.getHeight());
	System.out.println(canvas.getHeight());
	System.out.println(canvas.getWidth());
	setLocationRelativeTo(null);
    }
    
    /**
     * Cria o menu
     * 
     * @return o menu
     */
    public MenuBar getMenu() {
	MenuBar barraMenu = new MenuBar();
	Menu menuHelp = new Menu("Help");
	MenuItem opcoes = new MenuItem("Opções");
	opcoes.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		JOptionPane.showMessageDialog(null, renderer.getOpcoes());
	    }
	});
	Menu menuOpcoes = new Menu("Opções");
	MenuItem menuItemLimpar = new MenuItem("Limpar");
	menuItemLimpar.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		renderer.clear();
	    }
	});
	
	menuOpcoes.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		System.exit(0);
	    }
	});
	MenuItem menuItemSair = new MenuItem("Sair");
	
	menuOpcoes.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		System.exit(0);
	    }
	});
	menuHelp.add(opcoes);
	menuOpcoes.add(menuItemLimpar);
	menuOpcoes.add(menuItemSair);
	
	barraMenu.add(menuOpcoes);
	barraMenu.add(menuHelp);
	return barraMenu;
    }
}
